//header菜单栏的宽度调整

function ismobile() {
    //判断访问终端
    var browser = {
        versions: function () {
            var u = navigator.userAgent, app = navigator.appVersion;
            return {
                trident: u.indexOf('Trident') > -1, //IE内核
                presto: u.indexOf('Presto') > -1, //opera内核
                webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
                gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,//火狐内核
                mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
                android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, //android终端
                iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
                iPad: u.indexOf('iPad') > -1, //是否iPad
                webApp: u.indexOf('Safari') == -1, //是否web应该程序，没有头部与底部
                weixin: u.indexOf('MicroMessenger') > -1, //是否微信 （2015-01-22新增）
                qq: u.match(/\sQQ/i) == " qq" //是否QQ
            };
        }(),
        language: (navigator.browserLanguage || navigator.language).toLowerCase()
    };

    if (browser.versions.mobile == true) {
        if (browser.versions.iPhone == true) {
            return '1';
        } else if (browser.versions.android == true) {
            return '0';
        }
    } else {

    }
}


var isMobile = ismobile();

var belong_model_area = 400;

var token;

var uid;

//初始化首页
var curNavIndex = 0;//

var paramsArr = new Array(9);

var mescrollArr = new Array(9);//8个菜单所对应的8个mescroll对象

if (isMobile == 0) {

    window.android.getAndroidId("pp");

}

function WebViewDidFinishLoad() {

    var message = {

        "VcWay": "SendId"
    };

    window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

}
//token = "030BAFAB-D049-FE0D-3490-5EB3DEDC27D3";
//
//uid = 13;
//
//paramsArr[0] = initParams(uid);
//
//mescrollArr[0] = initMescroll("mescroll0", "dataList0", upCallback);


function getId() {

    if (isMobile == 1) {

        CallCapitalModel.jsCallOCWithSendId();

    } else if (isMobile == 0) {

        window.android.getAndroidId("pp");

    }
}

function receiveAndroidDetail(k) {

    //console.log(k)

    obj = $.parseJSON(k);

    token = obj.TOKEN;

    uid = obj.uid;

    paramsArr[0] = initParams(uid);

    mescrollArr[0] = initMescroll("mescroll0", "dataList0", upCallback);
}

function receiveiOSVcData(k) {

    //console.log(k)

    obj = $.parseJSON(k);
    //console.log(obj)

    token = obj.TOKEN;

    uid = obj.uid;

    paramsArr[0] = initParams(uid);

    mescrollArr[0] = initMescroll("mescroll0", "dataList0", upCallback);
}

function initParams(uid, type) {

    if (uid) {

        var params = new Params(1, 10, uid, type);

        return params;

    } else {

        WebViewDidFinishLoad()
    }


}

function Params(page, size, uid, type) {

    this.page = page;

    this.size = size;

    this.type = type;

    this.uid = uid;
}

$("#ul").on("click", "li>a", function () {

    $("#ul li  a").removeClass("active");

    $(this).addClass("active").css("text-decoration", "none");

    var index = Number($(this).data("index"));

    if (curNavIndex != index) {

        //隐藏当前列表和回到顶部按钮

        $("#mescroll" + curNavIndex).hide().addClass("hide");

        mescrollArr[curNavIndex].hideTopBtn();

        //显示对应的列表

        $("#mescroll" + index).show().removeClass("hide");

        //取出菜单所对应的mescroll对象,如果未初始化则初始化

        if (paramsArr[index] == null || mescrollArr[index] == null) {

            paramsArr[index] = initParams(uid, $(this).attr("id"));

            mescrollArr[index] = initMescroll("mescroll" + index, "dataList" + index, upCallback);

        } else {

            //检查是否需要显示回到到顶按钮

            var curMescroll = mescrollArr[index];

            var curScrollTop = curMescroll.getScrollTop();

            if (curScrollTop >= curMescroll.optUp.toTop.offset) {

                curMescroll.showTopBtn();

            } else {

                curMescroll.hideTopBtn();
            }
        }
        //更新标记
        curNavIndex = index;
    }
});

//上拉刷新，下拉加载
function initMescroll(mescrollId, clearEmptyId, upCallback) {

    var mescroll = new MeScroll(mescrollId, { //第一个参数"mescroll"对应上面布局结构div的id

        up: {

            callback: upCallback, //上拉加载的回调

            noMoreSize: 5,

            isBounce: false,

            clearEmptyId: clearEmptyId,

            empty: {

                icon: "../../person_home/img/wallet_img_no@3x.png", //图标,默认null

                tip: "暂无相关数据~  再逛逛~" //提示
            }
        }
    });

    return mescroll;
}
//上拉加载的回调
function upCallback(page) {

    var dataIndex = curNavIndex; //记录当前联网的nav下标,防止快速切换时,联网回来curNavIndex已经改变的情况;

    var params = paramsArr[dataIndex];

    params.page = page.num;

    params.size = page.size;

    //联网加载数据

    getListDataFromNet(dataIndex, params, function (data) {

        var listData = data.data;

        //必传参数(当前页的数据个数, 总数据量)

        if (data.codes == 200) {

            mescrollArr[dataIndex].endBySize(listData.list.length, listData.totalCount);

            listData.list.forEach(function (item) {//处理设备标签个数

                if (item.source == 100) {

                    if (item.goods_info.goods_tag.length > 3) {

                        item.goods_info.goods_tag.length = 3
                    }
                }
            });

            setListData(data, dataIndex);

        } else {

            mescrollArr[dataIndex].endBySize(0, 0);

            mescrollArr[dataIndex].showEmpty();

            $("#caseUl").hide();

        }

    }, function () { //联网失败的回调,隐藏下拉刷新和上拉加载的状态;

        mescrollArr[dataIndex].endErr();
    });
}

function setListData(data, dataIndex) {//没有的时候创建b标签 有的话创建i标签

    //console.log(data)

    var dataList = "dataList" + dataIndex;

    var html = template("listTpl", data);

    $("#" + dataList).append(html);

    data.data.list.forEach(function (item, i) {//处理点赞及样式  人力处理星级及标签  设备处理标签在U盘callback方法中
        switch (item.source) {

            case 200:

                for (var j = 0; j < item.goods_info.person_star; j++) {

                    $("#" + dataList + " .star" + i).find("b").eq(j).addClass("ssss")

                }
                if (item.goods_info.goods_tag.length > 3) {

                    item.goods_info.goods_tag.length = 3
                }

                break;

            case 300:
                if (item.make_info.like_info == 200) {//已点赞

                    $("#" + dataList + " .b" + i).addClass("yes")
                }
                break;
            case 500:
                if (item.dispersion_info.like_info == 200) {//已点赞

                    $("#" + dataList + " .b" + i).addClass("yes")
                }
                break;
            //case 600:
            //    if (item.site_info.like_info == 200) {//已点赞
            //
            //        //console.log("#" + dataList + " .b" + i)
            //
            //        $("#" + dataList + " .b" + i).addClass("yes")
            //    }
            //    break;
            case 700:
                if (item.incubator_info.like_info == 200) {//已点赞

                    $("#" + dataList + " .b" + i).addClass("yes")
                }

                break;
            case 800:
                if (item.advisory_info.like_info == 200) {//已点赞

                    $("#" + dataList + " .b" + i).addClass("yes")
                }
                break;
            case 900:

                if (item.team_info.case_type == 310) {

                    if (item.team_info.like_info == 200) {//已点赞

                        $("#" + dataList + " .b" + i).addClass("yes")
                    }
                } else if (item.team_info.case_type == 510) {

                    if (item.team_info.like_info == 200) {//已点赞

                        $("#" + dataList + " .b" + i).addClass("yes")
                    }
                }

                break;

        }
    });

    if (isiphoneX) {

        $(".footer_position").addClass("footer");

        $(".mescroll").css("padding-bottom", "0.484rem")
    } else {

        $(".footer_position").removeClass("footer")

    }

    var isClick = true;

    $(".zanP").on("click", function (e) {

        e.stopPropagation();

        if (isClick) {

            if ($(this).find(".zan").data("info") == 100) {

                like(100, uid, $(this).parents("li").data("id"), $(this).find(".zan").data("type"), $(this).find(".zan"));

            } else {

                like(200, uid, $(this).parents("li").data("id"), $(this).find(".zan").data("type"), $(this).find(".zan"));
            }

            isClick = !isClick;

        }
        setTimeout(function () {

            isClick = true;

        }, 1000)


    });

}
//从服务器获取数据
function getListDataFromNet(dataIndex, params, successCallback, errorCallback) {

    $.ajax({//

        url: url + 'common',

        type: "POST",

        dataType: "json",

        data: params,

        beforeSend: function (xhr) {

            xhr.setRequestHeader("CUSTOM-URL", "v1/incubator/resourceSupportClass");

            xhr.setRequestHeader("TOKEN", token)

        },

        success: function (data) {

            console.log(data);

            successCallback(data);

        },

        error: errorCallback
    });
}

$(".top_left").on("touchstart", "a", function (e) {

    e.preventDefault();

    $(".top_right>a").removeClass("active");

    $(this).addClass("active").css("text-decoration", "none");

    $("#action").show();

    $("#ul").show();

    $("#caseUl").hide();

    $("#case").hide();


});

//初始化项目加速

var curNavIndex1 = 0;

var paramsArr1 = new Array(3);

var mescrollArr1 = new Array(3);


$(".top_right").on("touchstart", "a", function (e) {

    e.preventDefault();

    $(".top_left>a").removeClass("active");

    $(this).addClass("active").css("text-decoration", "none");

    $("#case").show();

    $("#action").hide();

    $("#ul").hide();

    $("#caseUl").show();

    if (!paramsArr1[0]) {

        paramsArr1[0] = initParams(uid);

        mescrollArr1[0] = initMescroll("scroll0", "List0", downCallback);
    }


});

function downCallback(page) {

    //console.log(page);

    var dataIndex = curNavIndex1; //记录当前联网的nav下标,防止快速切换时,联网回来curNavIndex已经改变的情况;

    var params = paramsArr1[dataIndex];

    params.page = page.num;

    params.size = page.size;

    getListDataFromNet1(dataIndex, params, function (data) {

        var listData = data.data;

        if (data.codes == 200) {

            mescrollArr1[dataIndex].endBySize(listData.list.length, listData.totalCount);

            setListData1(data, dataIndex);

        } else {

            mescrollArr1[dataIndex].endBySize(0, 0);

            mescrollArr1[dataIndex].showEmpty();

            $("#action").hide();

            $("#ul").hide();

        }

    }, function () {

        mescrollArr1[dataIndex].endErr();

    })

}

function getListDataFromNet1(dataIndex, params, successCallback, errorCallback) {

    $.ajax({//项目加速列表

        url: url + 'common',

        type: "POST",

        dataType: "json",

        data: params,

        beforeSend: function (xhr) {

            xhr.setRequestHeader("CUSTOM-URL", "v1/fund/speedLists");

            xhr.setRequestHeader("TOKEN", token)

        },

        success: function (data) {

            console.log(data);

            successCallback(data);

        },

        error: errorCallback
    });

}

function setListData1(data, dataIndex) {

    var dataList = "List" + dataIndex;

    //console.log(dataList);

    var html = template("datalistTpl", data);

    $("#" + dataList).append(html);

    data.data.list.forEach(function (item, i) {//处理按钮

        var hh = item.status == 300 ? "已结束" : item.fund_type == 100 ? "去提案" : "去支持";

        $("#" + dataList + " .btn" + i).html(hh);


        if (item.status == 300 && item.operation_status == 200) {//处理已结束状态

            $("#" + dataList + " .btn" + i).removeClass("btn").css("color", "#ee5a50");

        } else if (item.status == 200 && item.operation_status == 200) {

            $("#" + dataList + " .btn" + i).css({"background": "#999", "color": "#fff"})

        } else if (item.status == 100 && item.operation_status == 200) {

            $("#" + dataList + " .btn" + i).css({"background": "#999", "color": "#fff"})

        } else if (item.status == 300 && item.operation_status == 100) {

            $("#" + dataList + " .btn" + i).removeClass("btn").css("color", "#ee5a50");

        } else if (item.status == 100 && item.operation_status == 100) {

            $("#" + dataList + " .btn" + i).css({"background": "#999", "color": "#fff"})

        }

        if (item.fund_type == 200) {//有进度条 处理宽度

            $("#" + dataList + " .progressWidth" + i).css("width", item.current_rate * 100 + "%");
        }

    });

    if (isiphoneX) {

        $(".mescroll").css("padding-bottom", "0.484rem")
    }

}

//资源支持 点击每个条目进详情
var isTrue = true;

$(".project_support").on("click", "li", function () {

    var target_id = $(this).data("targetid");

    var li_id = $(this).data("id");

    if (!li_id) return false;

    var json = null;

    switch (target_id) {
        case 100:

            json = {
                "title": "设备信息",

                "url": "https://viewapi.aipics.cn/view_h5/device/productMsg.html",

                "goods_id": li_id,

                "TOKEN": token
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "TerminalInformation",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectMsg(JSON.stringify(json))
            }

            break;
        case 200:

            json = {
                "title": "人力信息",

                "url": "https://viewapi.aipics.cn/view_h5/person/personMsg.html",

                "goods_id": li_id,

                "TOKEN": token
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "ManPowerInformation",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToPersonMsg(JSON.stringify(json))
            }

            break;
        case 300:

            json = {

                "title": "制作案例详情",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/make/makeDetail.html",

                "team_id": li_id,

                "TOKEN": token,

                "CallModelName": "CallFabricationDetailsModel"

            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "PublicWeb",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectFourModelDetail(JSON.stringify(json))
            }

            break;
        case 500:

            json = {
                "title": "宣发案例详情",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/xuanfa/xuanfaDetail.html",

                "team_id": li_id,

                "TOKEN": token,

                "CallModelName": "CallDiffusionDetailsModel"
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "DiffusionDetails",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectFourModelDetail(JSON.stringify(json))
            }

            break;
        case 600:

            json = {
                "title": "场地信息",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/area/areaView/areaMsg/areaMsg.html",

                "goods_id": li_id,

                "TOKEN": token,

                "CallModelName": "CallSiteInformationModel"
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "SiteInformation",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToFieldProjectMsg(JSON.stringify(json))
            }

            break;
        case 700:
            json = {
                "title": "投资方详情",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/bankroll/Investors.html",

                "team_id": li_id,

                "TOKEN": token,

                "CallModelName": "CallCapitalDetailsModel"
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "CapitalDetails",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectFundManageDetail(JSON.stringify(json))

            }

            break;
        case 800:

            json = {
                "title": "投资咨询详情",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/bankroll/Investment_consultation.html",

                "team_id": li_id,

                "TOKEN": token,

                "CallModelName": "CallCapitalDetailsModel"
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "CapitalDetails",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectFundManageDetail(JSON.stringify(json))
            }

            break;
        case 310:

            json = {
                "title": "制作团队详情",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/make/teamDetail.html",

                "team_id": li_id,

                "TOKEN": token,

                "belong_model": 310,

                "CallModelName": "CallFabricationDetailsModel"
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "CapitalDetails",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectFundManageDetail(JSON.stringify(json))

            }

            break;
        case 510:

            json = {
                "title": "宣发团队详情",

                "url": "https://viewapi.aipics.cn/view_h5/four_plates/xuanfa/teamDetail.html",

                "team_id": li_id,

                "TOKEN": token,

                "belong_model": 510,

                "CallModelName": "CallDiffusionDetailsModel"
            };

            if (isMobile == 1) {

                var message = {

                    "VcData": json,

                    "VcName": "CapitalDetails",

                    "VcWay": "PushVCName"
                };

                window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

            } else if (isMobile == 0) {

                window.android.jumpToProjectFundManageDetail(JSON.stringify(json))

            }

            break;
    }


});//点击每个条目跳详情

//项目加速 点击条目的图片进详情
$(".project_finance").on("click", "li .img", function () {

    var type = $(this).parents("li").data("id");//   100招标   200众筹

    var li_id = $(this).parents("li").attr("id");

    if (!li_id) return false;

    var json = null;

    if (type == 100) {

        json = {

            "title": "招标详情",

            "url": "https://viewapi.aipics.cn/view_h5/four_plates/bankroll/motion.html",

            "uid": uid,

            "id": li_id,

            "TOKEN": token
        };

        if (isMobile == 1) {

            var message = {

                "VcData": json,

                "VcName": "CapitalDetails",

                "VcWay": "PushVCName"
            };

            window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

        } else if (isMobile == 0) {

            window.android.jumpToProjectFundManageZhaoBiaoDetail(JSON.stringify(json))
        }


    } else if (type == 200) {

        json = {

            "title": "众筹详情",

            "url": "https://viewapi.aipics.cn/view_h5/four_plates/bankroll/crowdFundingDetail.html",

            "uid": uid,

            "id": li_id,

            "TOKEN": token,

            "CallModelName": "CallCapitalDetailsModel"


        };

        if (isMobile == 1) {

            var message = {

                "VcData": json,

                "VcName": "CapitalDetails",

                "VcWay": "PushVCName"
            };

            window.webkit.messageHandlers.CallCapitalModel.postMessage(message);


        } else if (isMobile == 0) {

            window.android.jumpToProjectFundManageZhaoBiaoDetail(JSON.stringify(json))
        }

    }


});

//项目加速中点击 去支持 去招标
var btnIsClick = true;

$(".project_finance").on("click", ".btn", function (e) {//灰色按钮不能点击处理一下

    e.stopPropagation();

    var fund_type = $(this).data("type");//100招标；200众筹；

    var isClick = $(this).data("isclick");//100可以；200不可以.


    if (isClick != 200) {//等于200不可点击
        if (btnIsClick) {

            if (fund_type == 100) {//100招标   去提案方案详情 你们的页面

                if (!$(this).parents("li").attr("id")) return false;

                var json = {

                    "tenderer_id": $(this).parents("li").attr("id")
                };

                if (isMobile == 1) {

                    var message = {

                        "VcData": json,

                        "VcName": "CapitalProposalRelease",

                        "VcWay": "PushVCName"
                    };

                    window.webkit.messageHandlers.CallCapitalModel.postMessage(message);


                } else if (isMobile == 0) {

                    window.android.jumpToProjectSubmitCaseDetail(JSON.stringify(json))
                }

            } else if (fund_type == 200) {//众筹  众筹方案详情 我的页面

                if (!$(this).parents("li").attr("id")) return false;

                var json = {
                    "title": "众筹详情",

                    "url": "https://viewapi.aipics.cn/view_h5/four_plates/bankroll/publicSchemeDetail.html",

                    "uid": uid,

                    "id": $(this).parents("li").attr("id"),

                    "TOKEN": token,

                    "CallModelName": "CallCapitalDetailsModel"
                };

                if (isMobile == 1) {

                    var message = {

                        "VcData": json,

                        "VcName": "CapitalDetails",

                        "VcWay": "PushVCName"
                    };

                    window.webkit.messageHandlers.CallCapitalModel.postMessage(message);

                } else if (isMobile == 0) {

                    window.android.jumpToProjectSupportListDetail(JSON.stringify(json));
                }

            }

            btnIsClick = false;

        }
        setTimeout(function () {

            btnIsClick = true;

        }, 1500);//点击后相隔多长时间可执行


    }
});

$("#caseUl").on("click", "li>a", function () {

    $("#caseUl li a").removeClass("active");

    $(this).css("text-decoration", "none").addClass("active");

    var index = Number($(this).data("index"));

    //console.log(index)

    if (curNavIndex1 != index) {

        //隐藏当前列表和回到顶部按钮

        $("#scroll" + curNavIndex1).hide().addClass("hide");

        mescrollArr1[curNavIndex1].hideTopBtn();

        //显示对应的列表

        $("#scroll" + index).show().removeClass("hide");

        //取出菜单所对应的mescroll对象,如果未初始化则初始化

        if (paramsArr1[index] == null || mescrollArr1[index] == null) {

            paramsArr1[index] = initParams(uid, $(this).data("id"));

            mescrollArr1[index] = initMescroll("scroll" + index, "List" + index, downCallback);

        } else {

            //检查是否需要显示回到到顶按钮

            var curMescroll = mescrollArr1[index];

            var curScrollTop = curMescroll.getScrollTop();

            if (curScrollTop >= curMescroll.optUp.toTop.offset) {

                curMescroll.showTopBtn();

            } else {

                curMescroll.hideTopBtn();
            }
        }
        //更新标记
        curNavIndex1 = index;
    }

});

function like(status, uid, target_id, belong_model_area, that) {

    $.ajax({

        url: url + 'common',

        type: "POST",

        dataType: "json",

        data: {
            "status": status,

            "uid": uid,

            "like_model": belong_model_area,

            "target_id": target_id
        },

        beforeSend: function (xhr) {

            xhr.setRequestHeader("CUSTOM-URL", "v1/fourModel/like");

            xhr.setRequestHeader("TOKEN", token)

        },
        success: function (result) {

            //console.log(result)

            if (result.codes == 200) {

                if (status == 200) {//取消

                    that.data("info", 100);

                    that.removeClass("yes");

                    that.siblings("span").html(Number(that.siblings("span").html()) - 1)

                } else if (status == 100) {//点赞

                    that.data("info", 200);

                    that.addClass("yes");

                    that.siblings("span").html(Number(that.siblings("span").html()) + 1)

                }
            }

        },
        error: function (info) {

            console.log(info)
        }

    })
}


